package com.saudidentalapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Responce;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.WebServiceManager;
import com.saudiapp.webservice.WebServiceManager.WebServiceListener;
import com.saudidentalapp.SpeakerAdapter.ContactInformationListener;
import com.saudidentalapp.datamodels.Speaker;

public class SpeakerActivity extends Activity implements WebServiceListener,OnItemClickListener,ContactInformationListener
{
	ListView lstSpeakers;
	Utilities utilities;
	 @Override
	protected void onCreate(Bundle savedInstanceState) 
	 {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_speakers);
		lstSpeakers=(ListView)findViewById(R.id.xlstSpeakers);
		setView();
	}
	 
	private void setView()
	{
		utilities=Utilities.getInstance(SpeakerActivity.this);
		utilities.showProgreessDialog();
		if(utilities.haveNetworkConnection())
		{
			WebServiceManager webServiceManager=new WebServiceManager(this);
			webServiceManager.setonWebServiceListener(this);
			webServiceManager.getSpeakers();
		}
		else
		{
		utilities.dismiss();
		utilities.showToast(Constants.INTERNET_NOT_AVAILABELE);
		}
		
		
		final Activity activity=this;
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				startActivity(new Intent(activity,TabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
			
	}

	@Override
	public void onProcesCompleted(int requestId, Object Data) 
	{	
		utilities.dismiss();
			@SuppressWarnings("unchecked")
			SpeakerAdapter adapter=new SpeakerAdapter(SpeakerActivity.this, R.layout.custom_speaker_row,((ArrayList<Speaker>)((Responce)Data).getResponceData()));
		if(adapter!=null)
			{
				lstSpeakers.setAdapter(adapter);
				adapter.setContactInformationListener(this);
				
				/*ArrayList<Speaker>programs=new ArrayList<Speaker>();
				programs=(ArrayList<Speaker>)((Responce)Data).getResponceData();
				for(int i=0;i<programs.size();i++)
				{
					if(!isProgramInSearchList(programs.get(i)))
					{
						Constants.Data.add(programs.get(i));
					}
				}*/		
				
		     }
		lstSpeakers.setOnItemClickListener(this);
	}
	
	
	boolean isProgramInSearchList(Speaker program)
	{
		boolean result=false;
		for(int i=0;i<Constants.Data.size();i++)
		{
			Object obect=Constants.Data.get(i);
			if(obect instanceof Speaker)
			{
				if(((Speaker)Constants.Data.get(i)).getID()==program.getID())
				{
					result=true;
					break;
				}
				else
				{
					result=false;
				}
			}
			else
			{
				result=false;
			}
		}
		
		return result;
	}

	@Override
	public void onErrorOccured(int requestId) 
	{
		utilities.dismiss();	
		utilities.showToast(Constants.ERROR_MESSAGE);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
	{
		Intent aboutSpeakerIntent=new Intent(SpeakerActivity.this, AboutSpeakers.class);	
		Speaker speaker=(Speaker)arg0.getAdapter().getItem(position);
		aboutSpeakerIntent.putExtra("MyClass",speaker);  
		startActivity(aboutSpeakerIntent);
		
	}

	@Override
	public void OnEmailClicked(String EmailTo, int position) 
	{			
		utilities.sendEmail(EmailTo, "", "");		
	}

	@Override
	public void OnCallClicked(String number, int position) 
	{		
		
		if(number!=null)
		{
			if(!number.equalsIgnoreCase("#")&& !number.equalsIgnoreCase(""))
			{	
				utilities.dialNumber(number);	
			}
		}
		else
		{
			utilities.showToast(""+Constants.DATA_NOT_AVAILABELE);
		}
		
	}
	 
}
